//
//  MCActivityStatisticsViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/25.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCActivityStatisticsViewController: MCViewController {
    
    public var activityName = "" // 活动名称
    public var activityType = 0  // 活动类型 8社区分销
    public var activityLogId = 0 // 发起活动id（活动实例id）
    
    private var expressDelivery : NSNumber = 0  // 快递送数量
    private var hasWriteOff : NSNumber = 0      // 已核销数量
    private var notWriteOff : NSNumber = 0      // 未核销数量
    private var allGoodsCount : NSNumber = 0    // 所有商品的数量
    private var allGoodsValue : NSNumber = 0    // 所有的营业额

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        managerClosure()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Closure
    func managerClosure() {
    }
    // MARK: - Action
    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "activityLogId" : activityLogId,
            "activityType"  : activityType
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_findActivityStatistics, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            let dataDict = response["data"] as? [String : AnyObject] ?? [:]
            
            self.expressDelivery = dataDict["expressDelivery"] as? NSNumber ?? 0
            self.hasWriteOff = dataDict["hasWriteOff"] as? NSNumber ?? 0
            self.notWriteOff = dataDict["notWriteOff"] as? NSNumber ?? 0

            let goodsList = dataDict["goodsList"] as? NSArray ?? []
            
            
            var value = 0
            var count = 0
            
            
            for dict in goodsList {
                let model = MCActivityStatisticsModel()
                model.setValuesForKeys(dict as? [String:Any] ?? [:])
                value += model.totalFee.intValue
                count += model.goodsNum.intValue
                self.dataArrayM.add(model)
            }
            self.allGoodsCount = NSNumber.init(value: count)
            self.allGoodsValue = NSNumber.init(value: value)
            
            self.tableView.reloadData()
            
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "活动统计"
        self.view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
        
        tableView.tableHeaderView = headView
        headView.nameLabel.text = "   " + activityName
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.grouped)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var headView: MCActivityStatisticsHeadView = {
        let view = MCActivityStatisticsHeadView()
        return view
    }()
}

extension MCActivityStatisticsViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //        return self.dataArrayM.count

        if section == 0 {
            return self.dataArrayM.count + 1
        } else {
            return 4
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 88
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = MCActivityStatisticsSectionView()
        
        if section == 0 {
            view.titleLabel.text = "   商品"
            view.nameLabel.text = "商品名称"
            view.countLabel.text = "商品数量"
            view.explainButton.setTitle("营销额", for:.normal)
        } else {
            view.titleLabel.text = "   订单"
            view.nameLabel.text = "订单状态"
            view.countLabel.text = "订单数量"
            view.explainButton.setTitle("明细", for:.normal)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        return view
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCActivityStatisticsCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.backgroundColor = UIColor.MCGray_light
        cell.closure = { vc in
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
        }
        cell.goodsCount = self.dataArrayM.count
        cell.allGoodsCount = allGoodsCount
        cell.expressDelivery = self.expressDelivery
        cell.hasWriteOff = self.hasWriteOff
        cell.notWriteOff = self.notWriteOff
        cell.allGoodsValue = allGoodsValue
        cell.activityLogId = activityLogId
        
        cell.indexPath = indexPath

        if indexPath.section == 0 {
            if indexPath.row <= (self.dataArrayM.count - 1) {
                cell.model = self.dataArrayM[indexPath.row] as! MCActivityStatisticsModel
            }
        } else {
            
        }
        

        return cell
    }
}

